﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Agile.Service;
using Agile.Service.Rbac;

using Microsoft.AspNetCore.Mvc;

namespace Web.Areas.Rbac.Controllers
{
    [Area("rbac")]
    public class RoleController : BaseController<Role>
    {
        private readonly IRoleService _roleService;
        private readonly IMenuService _menuService;
        private readonly IMenuElementService _menuelementService;
        private readonly IRelevanceService _relevanceService;
        public RoleController(IRoleService roleService,
            IMenuService menuService,
            IMenuElementService menuelementService,
            IRelevanceService relevanceService)
        {
            _roleService = roleService;
            _menuService = menuService;
            _menuelementService = menuelementService;
            _relevanceService = relevanceService;
        }
        [HttpPost]
        public override IActionResult Switch(string keyId)
        {
            _roleService.Switch(keyId);
            return Success("操作成功。");
        }
        public override IActionResult GetGridJson(string keyword)
        {
            var data = _roleService.GetRoles(keyword);
            return Content(data.ToJson());
        }
        public override IActionResult DeleteForm(string keyValue)
        {
            _relevanceService.UnAssign(new AssignReq { type = Define.ROLEMODULE, firstId = keyValue });
            _relevanceService.UnAssign(new AssignReq { type = Define.ROLEELEMENT, firstId = keyValue });
            return base.DeleteForm(keyValue);
        }
        public IActionResult SaveRole(Role role, string permissionIds)
        {
            role.Category = 1;
            if (!string.IsNullOrWhiteSpace(permissionIds))
            {
                _relevanceService.UnAssign(new AssignReq { type = Define.ROLEMODULE, firstId = role.ID });
                _relevanceService.UnAssign(new AssignReq { type = Define.ROLEELEMENT, firstId = role.ID });
                var moduledata = _menuService.GetByAll();
                var buttondata = _menuelementService.GetByAll();
                string[] permissionIdArr = permissionIds.Split(',');
                List<string> menus = new List<string>();
                List<string> menuelement = new List<string>();
                foreach (var itemId in permissionIdArr)
                {
                    if (moduledata.Find(t => t.ID == itemId) != null)
                    {
                        menus.Add(itemId);
                    }
                    if (buttondata.Find(t => t.ID == itemId) != null)
                    {
                        menuelement.Add(itemId);
                    }
                }
                _relevanceService.Assign(new AssignReq { type = Define.ROLEMODULE, firstId = role.ID, secIds = menus.ToArray() });
                _relevanceService.Assign(new AssignReq { type = Define.ROLEELEMENT, firstId = role.ID, secIds = menuelement.ToArray() });
            }
            return base.SubmitForm(role);
        }

    }
}